<template>
  <view v-if="goods_info.goods_name" class="goods-detail-container ">
    <!-- 轮播图 -->
    <swiper class="swiper" circular indicator-dots="true" autoplay="true" interval="2000" duration="500"
      indicator-active-color="#ff007f">
      <swiper-item v-for="(item,i) in goods_info.pics" :key="i">
        <image class="swiper-item" :src="item.pics_big_url"></image>
      </swiper-item>
    </swiper>
    <!-- 商品名称 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="goods-price">￥{{goods_info.goods_price}}</view>
      <!-- 商品主体信息 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{goods_info.goods_name}}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="20" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
    </view>
    <!-- 快递·免运费 -->
    <view class="yf">快递·免运费</view>
    <!-- 商品图片介绍 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 商品导航栏 -->
    <view class="goods-nav">
      <!-- fill 控制右侧按钮的样式 -->
      <!-- options 左侧按钮的配置项 -->
      <!-- buttonGroup 右侧按钮的配置项 -->
      <!-- click 左侧按钮的点击事件处理函数 -->
      <!-- buttonClick 右侧按钮的点击事件处理函数 -->
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      total: {
        handler(newVal) {
          const findResult = this.options.find(x => x.text === "购物车")
          if (findResult) {
            findResult.info = newVal
          }
        },
        immediate: true
      }
    },
    data() {
      return {
        options: [{
          icon: 'headphones',
          text: '客服'
        }, {
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ],
        goods_info: {}, //商品信息
      };
    },
    async onLoad(options) {
      const {
        data: res
      } = await uni.$http.get('/goods/detail?goods_id=' + options.goods_id)

      if (res.meta.status !== 200) return uni.$ShowMsg()
      // 对商品介绍图片进行处理
      res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;"')

      this.goods_info = res.message
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      // 左侧按钮的点击事件处理函数
      onClick(e) {
        if (e.content.text === "购物车") {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      // 右侧按钮的点击事件处理函数
      buttonClick(e) {
        if (e.content.text === "加入购物车") {
          // 2. 组织一个商品的信息对象
          const goods = {
            goods_id: this.goods_info.goods_id, // 商品的Id
            goods_name: this.goods_info.goods_name, // 商品的名称
            goods_price: this.goods_info.goods_price, // 商品的价格
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goods_info.goods_small_logo, // 商品的图片
            goods_state: true // 商品的勾选状态
          }
          // 3. 通过 this 调用映射过来的 addToCart 方法，把商品信息对象存储到购物车中
          this.addToCart(goods)
        }
      }
    }
  }
</script>

<style lang="scss">
  .goods-detail-container {
    padding-bottom: 50px;
  }

  .swiper {
    height: 750rpx;

    .swiper-item {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 10px;

    .goods-price {
      color: #c00000;
      font-size: 20px;
      font-weight: bold;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;


      .goods-name {
        font-size: 13px;
        padding-right: 5px;
      }

      .favi {
        width: 120px;
        border-left: 1px solid #efefef;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: gray;
        font-size: 13px;
      }
    }
  }

  .yf {
    margin: 5px 10px;
    color: gray;
    font-size: 12px;
  }

  .goods-nav {
    // 为商品导航组件添加固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>